package com.haliyoo.adhs.dao.impl;

import java.util.Date;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.apache.log4j.Logger;

import com.haliyoo.adhs.dao.UserDao;
import com.haliyoo.adhs.exception.DaoLevelException;
import com.haliyoo.adhs.model.User;

public class UserDaoImpl implements UserDao {
	
	private EntityManager em;

    @PersistenceContext
    public void setEntityManager(EntityManager em) {
        this.em = em;
    }
    @SuppressWarnings("unchecked")
    public List<User> findAll() throws DaoLevelException {
    	List<User> userList = null;
    	try{
	        Query query = getEntityManager().createQuery("select p FROM User p");
	        userList = query.getResultList();
	        
	    }catch(Exception e){
	    	throw new DaoLevelException("error -- List<User> findAll()",e);
	    }
	    return userList;
    }

    public void save(User user) throws DaoLevelException {
        if (user.getUserId() == null) {
            
        	user.setCreateTime(new Date());
        	user.setRankId(Short.valueOf("1"));
        	user.setWeiboFlag(Short.valueOf("0"));
        	try{
	            em.persist(user);// new
	        }catch(Exception e){
		    	throw new DaoLevelException("error -- save(User user) -- em.persist(user)",e);
		    }
        } else {
        	try{
	            em.merge(user); // update
	        }catch(Exception e){
		    	throw new DaoLevelException("error -- save(User user) -- em.merge(user)",e);
		    }
        }
    }

    public void remove(User user) throws DaoLevelException {
    	 if (user != null) {
	    	try{
	    		em.remove(em.merge(user));
	        }catch(Exception e){
	        	throw new DaoLevelException("error -- remove(User user)",e);
		    }
	    }
    }

    private EntityManager getEntityManager() {
        return em;
    }

    public User find(Integer id) throws DaoLevelException {
    	User user = null;
    	try{
    		user =  em.find(User.class, id);
	    }catch(NoResultException e){
	    	throw new DaoLevelException("error -- find(Integer id)",e);
	    }
	    return user;
    }
    
    public User findByName(String userName) throws DaoLevelException{
    	User user = null;
		try{
			Query query = getEntityManager().createQuery("select p FROM User p where p.userName=:userName");
			query.setParameter("userName", userName);
			if(query.getSingleResult() != null ){
				user = (User) query.getSingleResult();
			}
			
	    }catch(NoResultException e){
	    	throw new DaoLevelException("error -- findByName(String userName)",e);
	    }
		return user;
    }
	@Override
	public User findByWeiboUserId(Integer weiboUserId, Short weiboFlag)
			throws DaoLevelException {
		User user = null;
		try{
			Query query = getEntityManager().createQuery("select p FROM User p where p.weiboUserId=:weiboUserId and p.weiboFlag=:weiboFlag");
			query.setParameter("weiboUserId", weiboUserId);
			query.setParameter("weiboFlag", weiboFlag);
			if(query.getSingleResult() != null ){
				user = (User) query.getSingleResult();
			}
			
	    }catch(NoResultException e){
	    	throw new DaoLevelException("error -- findByWeiboUserId(Integer weiboUserId, Short weiboFlag)",e);
	    }
		return user;
	}
}